package com.jfinalplus.plugin.spring;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

import com.jfinal.plugin.activerecord.IDataSourceProvider;

public class SpringDataSourceProvider implements IDataSourceProvider {
	
	private static final String proxyDsName = "proxyDataSource";

	private SpringPlugin springPlugin;
	
	private String beanName;

	public SpringDataSourceProvider(SpringPlugin springPlugin, String beanName) {
		this.springPlugin = springPlugin;
		this.beanName = beanName;
	}
	public SpringDataSourceProvider(SpringPlugin springPlugin) {
		this.springPlugin = springPlugin;
		this.beanName = proxyDsName;
	}

	public DataSource getDataSource() {
		ApplicationContext ctx = springPlugin.getApplicationContext();
		return (DataSource)ctx.getBean(beanName,TransactionAwareDataSourceProxy.class);
	}
}
